package com.atguigu.demojuc.chap06;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author njg
 * @version 1.0
 * @date 2025/6/17 19:57
 */

class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        Thread.sleep(3000);
        System.out.println(Thread.currentThread().getName() + "\t" + "come--in");
        return "ok";
    }
}
public class MyCallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable myCallable = new MyCallable();
        FutureTask<String> futureTask = new FutureTask<>(myCallable);
//        FutureTask<String> futureTask2 = new FutureTask<>(myCallable);

        Thread thread = new Thread(futureTask, "callable");
        thread.start();

        /*Thread.sleep(1000);
        boolean iscancal = futureTask.cancel(true);
        if (iscancal){
            System.out.println(Thread.currentThread().getName() + "\t" + "被取消了");
        }*/


        while (!futureTask.isDone()) {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "\t" + "好了吗————");

        }
        String result = futureTask.get();
//        new Thread(futureTask2,"callable2").start();
//        String result2 = futureTask2.get();
        System.out.println(result);
//        System.out.println(result2);
    }
}
